package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppMenuResource;
import com.irdstudio.apicenter.console.service.authenticate.AppMenuResourceService;

/**
 * app应用管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appMenuResource")
public class AppMenuResourceAction extends MacmsBaseAction {
	@Autowired
	private AppMenuResourceService appMenuResourceService;

	@RequestMapping(value = "jumpAddMenuResourcePage")
	public String jumpAddMenuResourcePage(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String appId = request.getParameter("appid");
		request.setAttribute("appid", appId);
		return "auth/addMenu";
	}

	@RequestMapping(value = "addMenu")
	public void addMenu(
			@ModelAttribute("appMenuResource") AppMenuResource appMenuResource,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appMenuResourceService.insertOne(appMenuResource);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "jumpUpdateMenu")
	public String jumpUpdateMenu(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String flag = request.getParameter("flag");
		String appId = request.getParameter("appId");
		String menuId = request.getParameter("menuId");
		AppMenuResource appMenuResource = appMenuResourceService
				.queryAppMenuDetail(appId, menuId);
		request.setAttribute("appMenuResource", appMenuResource);
		if ("1".equals(flag)) {
			return "auth/updateMenuResource";
		} else {
			return "auth/queryMenuResourceDetail";
		}

	}

	@RequestMapping(value = "updateMenu")
	public void updateMenu(
			@ModelAttribute("appMenuResource") AppMenuResource appMenuResource,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appMenuResourceService.update(appMenuResource);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "deleteMenu")
	public void deleteMenu(
			@ModelAttribute("appMenuResource") AppMenuResource appMenuResource,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		String appId = request.getParameter("appId");
		String menuId = request.getParameter("menuId");
		flag = appMenuResourceService.delete(appId, menuId);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "queryMenu")
	public void queryMenu(
			@ModelAttribute("appMenuResource") AppMenuResource appMenuResource,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		List<AppMenuResource> list = new ArrayList<AppMenuResource>();
		String appId = request.getParameter("appid");
		String appRoleId = request.getParameter("roleid");
		list = appMenuResourceService.queryMenusByRoleId(appRoleId, appId);

		String jsonResult = JSON.toJSONString(list);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	@RequestMapping(value = "saveRoleMenu")
	public void saveRoleMenu(
			@ModelAttribute("appMenuResource") AppMenuResource appMenuResource,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		String appId = request.getParameter("appid");
		String appRoleId = request.getParameter("roleid");
		String menuStr = request.getParameter("menuStr");

		flag = appMenuResourceService
				.insertRoleRight(menuStr, appId, appRoleId);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

}
